package bd;

import services.AdministrarDespacho;
import sessionBeans.AdministradorModulo;
import sessionBeans.AdministradorModuloBean;
import xml.RecibirSolicitudXML;
import xml.RespuestaXML;

import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;

import dtos.ModuloDTO;
import dtos.OrdenDespachoDTO;
import dtos.OrdenPedidoDTO;
import dtos.ServiciosModuloDTO;
import dtos.SolicitudDTO;
import exceptions.EntityAlreadyExistException;

public class BusinessDelegate {
	private static BusinessDelegate instance;
	private AdministrarDespacho despachoFacade;
	private InitialContext initialContext;
	
	final String appName = "DespachoEAR";
	final String moduleName = "Despacho";
	
	final String sessionBeanName = "AdministrarDespachoBean";
	final String viewClassName = AdministrarDespacho.class.getName();
	
	private BusinessDelegate() {
		inicializarContexto();
	}

	public static BusinessDelegate getInstance() {
		if (instance == null)
			instance = new BusinessDelegate();
		return instance;
	}
	private AdministrarDespacho getFacadeRemote() {
		try {
			if (despachoFacade == null) {
				despachoFacade = (AdministrarDespacho) initialContext.lookup("ejb:"+appName + "/" + moduleName + "/" + sessionBeanName + "!"
						+ viewClassName);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return despachoFacade;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void inicializarContexto() {
		try {
			Hashtable jndiProps = new Hashtable();
			jndiProps.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");
			jndiProps.put(Context.SECURITY_PRINCIPAL, "test");
			jndiProps.put(Context.SECURITY_CREDENTIALS, "test1234.");
			jndiProps.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
			initialContext = new InitialContext(jndiProps);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/*
	 * LISTA METODOS
	 */
	
	public List<ModuloDTO> getModulos(){
		return getFacadeRemote().getAllModulo();
	}
	
	public ModuloDTO getModulo(int id){
		return getFacadeRemote().getModulo(id);
	}
	
	public List<SolicitudDTO> getSolicitudesPendientes(){
		return getFacadeRemote().getSolicitudesPendientes();
	}
	
	public ServiciosModuloDTO getServicio(int tipo, int idModulo){
		return getFacadeRemote().buscarServicio(tipo, idModulo);
	}

	public ModuloDTO getModuloActivo(String modulo) {
		// TODO Auto-generated method stub
		return getFacadeRemote().getModuloActivo(modulo);
	}

	public RespuestaXML recibirSolicitudesJSON(RecibirSolicitudXML str) {
		// TODO Auto-generated method stub
		return getFacadeRemote().recibirSolicitudesJSON(str);
	}
	
	public boolean guardarModulo(ModuloDTO mod){
		return getFacadeRemote().guardarModulo(mod);
	}
	
	public void CrearOrdenDespacho(OrdenPedidoDTO orden) throws EntityAlreadyExistException
	{
		 getFacadeRemote().CrearOrdenDeDespacho(orden);
	}

	public List<SolicitudDTO> getSolicitudesPorEstado(int i) {
		// TODO Auto-generated method stub
		return getFacadeRemote().getSolicitudesPorEstado(i);
	}

	public List<OrdenDespachoDTO> getOrdenesDespacho() {
		// TODO Auto-generated method stub
		return getFacadeRemote().GetAllOrdenesDespacho();
	}
}
